import os.path
import sys
import SCons.Action
import SCons.Builder
import SCons.Util

def generate(env):
    """
    Add a Builder factory function and construction variables for
    WGET'ing to an Environment.
    """

    def WGetFactory(url, env=env):
        """ """
        env['WGETCOM'] = '$WGET $WGETFLAGS -O $TARGET $WGETURL/${TARGET.file}' + \
                         ' || (rm -f $TARGET && exit 1)'
        env['WGETCOMSTR'] = '$WGET $WGETFLAGS -O $TARGET $WGETURL/${TARGET.file}'
        act = SCons.Action.Action('$WGETCOM', '$WGETCOMSTR')
        return SCons.Builder.Builder(action = act, env = env, WGETURL=url)

    env.WGet = WGetFactory

    env['WGET'] = 'wget'
    env['WGETFLAGS'] = SCons.Util.CLVar('')

def exists(env):
    return env.Detect('wget')
